package fem2.mesh_and_topology;

/**
 * OctTree class for 3d indexing
 * 
 * @author hbui
 * 
 */
public class OctTree extends SpatialTree {

	private double x0, y0, z0;

	public OctTree(double x0, double y0, double z0, double size) {
		this.x0 = x0;
		this.y0 = y0;
		this.z0 = z0;
		this.size = size;
	}

	@Override
	public boolean isInside(double... p) {
		return p[0] >= x0 && p[0] <= x0 + size && p[1] >= y0 && p[1] <= y0 + size && p[2] >= z0
				&& p[2] <= z0 + size;
	}

	@Override
	public void subdivide() {
		branches = new OctTree[8];
		branches[0] = new OctTree(x0, y0, z0, size / 2);
		branches[1] = new OctTree(x0 + size / 2, y0, z0, size / 2);
		branches[2] = new OctTree(x0 + size / 2, y0 + size / 2, z0, size / 2);
		branches[3] = new OctTree(x0, y0 + size / 2, z0, size / 2);
		branches[4] = new OctTree(x0, y0, z0 + size / 2, size / 2);
		branches[5] = new OctTree(x0 + size / 2, y0, z0 + size / 2, size / 2);
		branches[6] = new OctTree(x0 + size / 2, y0 + size / 2, z0 + size / 2, size / 2);
		branches[7] = new OctTree(x0, y0 + size / 2, z0 + size / 2, size / 2);
	}

	@Override
	public boolean isIntersected(SpatialCriteria criteria) {
		return criteria.isIntersected(x0, y0, z0, size);
	}
}
